#include <opencv2/opencv.hpp>
#include <zbar.h> // zbar库 sudo apt-get install libzbar0 libzbar-dev
#include <iostream>

int main(int argc, char** argv) {
    // if (argc != 2) {
    //     std::cerr << "用法: " << argv[0] << " <图片文件路径>" << std::endl;
    //     return -1;
    // }

    // 读取图像
    cv::Mat frame = cv::imread("/home/firefly/Pictures/bar11.png", cv::IMREAD_COLOR);
    if (frame.empty()) {
        std::cerr << "无法读取图像: " << argv[1] << std::endl;
        return -1;
    }

    zbar::ImageScanner scanner;
    scanner.set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 1);


    cv::Mat gray;
    cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);

    // 将OpenCV图像转换为ZBar图像
    zbar::Image image(frame.cols, frame.rows, "Y800", gray.data, frame.cols * frame.rows);

    // 扫描图像
    int n = scanner.scan(image);

    // 遍历检测到的符号
    for (auto symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol) {
        std::cout << "解码类型: " << symbol->get_type_name() << std::endl;
        std::cout << "解码数据: " << symbol->get_data() << std::endl;

        // 在图像上绘制矩形框
        std::vector<cv::Point> points;
        for (int i = 0; i < symbol->get_location_size(); i++) {
            points.push_back(cv::Point(symbol->get_location_x(i), symbol->get_location_y(i)));
        }
        cv::polylines(frame, points, true, cv::Scalar(0, 255, 0), 2);
    }

    // 显示图像
    cv::imshow("QRCode Scanner", frame);
    cv::waitKey(0);

    return 0;
}